export default {
  data () {
    return {
      tableData: [
        {
          roleName: 'catlu',
          roleDesc: '技术负责人'
        }
      ],
      keys: [],
      tree: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      editData: [],
      dialogVisible: false
    }
  },
  methods: {
    async getUserList () {
      let res = await this.$axios.get('roles')
      this.tableData = res.data.data
    },
    indexMethod (index) {
      return index
    },
    /* 展示编辑角色权限 */
    async showDialog (row) {
      this.dialogVisible = true
      this.editData = row
      let num = []
      row.children.forEach(item1 => {
        item1.children.forEach(item2 => {
          item2.children.forEach(item3 => {
            num.push(item3.id)
          })
        })
      })
      console.log(num)
      this.keys = num
      let res = await this.$axios.get('rights/tree')
      this.tree = res.data.data
    },
    /* 提交更新分配权限 */
    async updataAuthority () {
      let id = this.editData.id

      let key1 = this.$refs.tree.getHalfCheckedKeys()
      let key2 = this.$refs.tree.getCheckedKeys()
      let keyArr = key1.concat(key2)

      let res = await this.$axios.post(`roles/${id}/rights`, { rids: keyArr.join(',') })
      if (res.data.meta.status === 200) {
        this.$message.success(res.data.meta.msg)
      } else { this.$message.error(res.data.meta.msg) }
      // console.log(res)
      this.getUserList()
      this.dialogVisible = false
    }
  },
  created () {
    this.getUserList()
  }
}
